using System;
using Animancer;
using Cysharp.Threading.Tasks;
using MoonFramework.Scripts.Event;
using MoonFramework.Template;
using MoonFramework.View;
using Serilog;
using UnityEngine.SceneManagement;

namespace MoonFramework.Scripts.Scences
{
    /// <summary>
    ///     场景管理模板
    /// </summary>
    public class ScenesManager : BaseManager<ScenesManager>
    {
        private const string AnimationName = "ScenesChange";
        
        public ScenesManager()
        {
            AnimancerManager.Instance.Allocate(AnimationName);
        }

        public void Init(params Tuple<string, ClipTransition>[] tuples)
        {
            for (int i = 0; i < tuples.Length; i++)
            {
                AnimancerManager.Instance.Add(AnimationName, tuples[i].Item1, tuples[i].Item2);
            }
        }
        
        /// <summary>
        ///     切换场景
        /// </summary>
        /// <param name="name">场景名称</param>
        /// <param name="action">回调方法</param>
        public void LoadScene(string name, Action fun = null)
        {
            // 同步加载场景
            SceneManager.LoadScene(name);
            // 场景加载完成后执行回调
            fun?.Invoke();
        }

        /// <summary>
        /// 异步加载场景
        /// </summary>
        /// <param name="name"></param>
        /// <param name="fun"></param>
        public async UniTaskVoid LoadSceneAsync(string name, Action fun = null)
        {
            // 使用 Progress 来封装进度回调逻辑
            var progress = new Progress<float>(p =>
            {
                EventCenter.Instance.Callback("LoadingSceneProgress", p);
            });

            // 异步加载场景并等待完成
            await SceneManager.LoadSceneAsync(name).ToUniTask(progress);   //等待场景加载，否则调用回调可能会问题

            // 加载完成后执行回调
            EventCenter.Instance.Callback("LoadSceneSucceed");
            fun?.Invoke();
        }
        
        /// <summary>
        /// 异步场景卸载
        /// </summary>
        /// <param name="name"></param>
        /// <param name="fun"></param>
        public async UniTaskVoid UnloadSceneAsync(string name, Action fun = null)
        {
            // 检查场景是否已加载
            if (!SceneManager.GetSceneByName(name).isLoaded)
            {
                Log.Error($"场景{name}未加载！");
                return;
            }

            // 异步卸载场景
            var ao = SceneManager.UnloadSceneAsync(name);
            await ao.ToUniTask();   //注意等待场景卸载，否则调用回调可能出问题

            // 卸载完成后执行回调
            fun?.Invoke();

            Log.Debug("场景{0}已经卸载", name);
        }
        
        /// <summary>
        /// 允许多个场景同时加载
        /// </summary>
        /// <param name="name"></param>
        /// <param name="fun"></param>
        public async UniTaskVoid LoadSceneAdditiveAsync(string name, Action fun = null)
        {
            var progress = new Progress<float>(p =>
            {
                EventCenter.Instance.Callback("加载进度更新", p);
            });

            // 异步加载 Additive 模式场景
            var ao = SceneManager.LoadSceneAsync(name, LoadSceneMode.Additive);
            await ao.ToUniTask(progress);

            // 加载完成后执行回调
            fun?.Invoke();
        }
        

        /// <summary>
        /// 异步加载并且播放动画
        /// </summary>
        /// <param name="name"></param>
        /// <param name="animationName"></param>
        /// <param name="animation"></param>
        /// <param name="fun"></param>
        public async UniTaskVoid LoadSceneAsync_Play(string sceneName,string animationName,ClipTransition animation, Action fun = null)
        {
            // 显示过渡动画
            AnimancerManager.Instance.Play(AnimationName,animationName);
            // 异步加载场景
            LoadSceneAsync(sceneName, fun).Forget();

            // 隐藏过渡动画
            AnimancerManager.Instance.Stop(AnimationName);
        }
    }
}